package com.btpj.jetpack.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.paging.PagedList
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.btpj.jetpack.R
import com.btpj.jetpack.databinding.LayoutBaseListBinding
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.layout_base_list.*
import org.koin.androidx.viewmodel.ext.android.getViewModel
import kotlin.reflect.KClass

/**
 * 列表类Fragment的基类
 *
 * @author LTP 2020/7/21
 */
abstract class BaseListFragment<T, VM : BaseListViewModel<T>> : Fragment(),
    OnRefreshLoadMoreListener {

    abstract fun generateAdapter(): PagedListAdapter<T, RecyclerView.ViewHolder>

    val mAdapter by lazy { generateAdapter() }
    lateinit var mViewModel: VM
    lateinit var mBinding: LayoutBaseListBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(
            inflater,
            R.layout.layout_base_list,
            container,
            false
        )

        val decoration = DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
        decoration.setDrawable(
            ContextCompat.getDrawable(
                requireContext(),
                R.drawable.list_divier_home
            )!!
        )
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(requireContext())
            addItemDecoration(decoration)
            adapter = mAdapter
        }

        mBinding.smartRefreshLayout.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshLoadMoreListener(this@BaseListFragment)
        }

        initViewModel()
        onCreateViewAfter()
        return mBinding.root
    }

    open fun onCreateViewAfter() {}

    private fun initViewModel() {
        mViewModel =
            getViewModel(this.javaClass.kotlin.supertypes[0].arguments[1].type!!.classifier!! as KClass<VM>)
        mViewModel.pageData.observe(viewLifecycleOwner, Observer {
            submitPageList(it)
        })
    }

    fun submitPageList(pagedList: PagedList<T>) {
        mAdapter.submitList(pagedList)
    }

    fun finishRefresh() {
        val state = smartRefreshLayout.state
        if (state.isOpening) {
            if (state.isHeader) {
                smartRefreshLayout.finishRefresh()
            } else if (state.isFooter) {
                smartRefreshLayout.finishLoadMore()
            }
        }
    }

    fun hiddenActionBar() {
        mBinding.actionBar.visibility = View.GONE
    }
}